﻿Option Strict On
Option Explicit On
Option Infer On

Imports System
Imports System.Linq
Imports System.Collections.Generic
Imports GenDotNet.ObjectModel
Imports <xmlns="http://kadgen.com/tempCode">


Public Class NetTypeInfo

   Private mStandardNetType As StandardNetType
   Private mTypeInfo As TypeInfo

#Region "Constructors"

   Public Sub New(ByVal name As String)
      mTypeInfo = New TypeInfo(name, Scope.Public)
   End Sub

   Public Sub New(ByVal name As String, ByVal ParamArray genericInfos() As GenericInfo)
      mTypeInfo = New TypeInfo(name)
      mTypeInfo.Generics.AddRange(genericInfos)
   End Sub

   Public Sub New(ByVal name As String, ByVal ParamArray genericInfos() As String)
      mTypeInfo = New TypeInfo(name)
      For Each info In genericInfos
         mTypeInfo.Generics.Add(New GenericInfo(info))
      Next
   End Sub

   Public Sub New(ByVal name As String, ByVal ParamArray typeInfos() As NetTypeInfo)
      mTypeInfo = New TypeInfo(name)
      For Each info In typeInfos
         Dim genericName = If(info.TypeInfo Is Nothing, info.StandardNetType.ToString(), TypeInfo.Name)
         Dim genericInfo = New GenericInfo(genericName)
         mTypeInfo.Generics.Add(genericInfo)
      Next
   End Sub

   Public Sub New(ByVal standardNetType As StandardNetType)
      Me.mStandardNetType = standardNetType
   End Sub

   Public Sub New(ByVal typeInfo As TypeInfo)
      Me.mTypeInfo = typeInfo
   End Sub

#End Region

#Region "Operators"
   Public Shared Widening Operator CType( _
            ByVal standardType As StandardNetType) _
            As NetTypeInfo
      Return New NetTypeInfo(standardType)
   End Operator

   Public Shared Widening Operator CType( _
            ByVal type As String) _
            As NetTypeInfo
      Return New NetTypeInfo(type)
   End Operator
#End Region

#Region "Properies"
   Public Property StandardNetType() As StandardNetType
      Get
         Return mStandardNetType
      End Get
      Set(ByVal value As StandardNetType)
         mStandardNetType = value
      End Set
   End Property

   Public Property TypeInfo() As TypeInfo
      Get
         Return mTypeInfo
      End Get
      Set(ByVal value As TypeInfo)
         mTypeInfo = value
      End Set
   End Property
#End Region


End Class

Public Module StandardNetTypeExtensions
   <System.Runtime.CompilerServices.Extension()> _
Public Function GetDataReaderFunction(ByVal type As StandardNetType) As String
      Select Case type
         Case StandardNetType.Single
            Return "GetFloat"
         Case Else
            Return "Get" & type.ShortName
      End Select
      '<xsl:when test="@ShortNETType='Guid'">GetGuid</xsl:when>
      '<xsl:when test="@ShortNETType='Int16'">GetInt16</xsl:when>
      '<xsl:when test="@ShortNETType='Int32'">GetInt32</xsl:when>
      '<xsl:when test="@ShortNETType='Int64'">GetInt64</xsl:when>
      '<xsl:when test="@ShortNETType='String'">GetString</xsl:when>
      '<xsl:when test="@ShortNETType='DateTime'">GetDateTime</xsl:when>
      '<xsl:when test="@ShortNETType='Single'">GetFloat</xsl:when>
      '<xsl:when test="@ShortNETType='Decimal'">GetDecimal</xsl:when>
      '<xsl:when test="@ShortNETType='Byte'">GetByte</xsl:when>
      '<xsl:when test="@ShortNETType='Double'">GetDouble</xsl:when>
      '<xsl:when test="@ShortNETType='Boolean'">GetBoolean</xsl:when>
   End Function


End Module

Namespace VB
   Public Module NetTypeInfoExtensions
      <System.Runtime.CompilerServices.Extension()> _
      Public Function Output(ByVal netType As NetTypeInfo) As String
         If netType.TypeInfo IsNot Nothing Then
            Return netType.TypeInfo.OutputName
         Else
            Return netType.StandardNetType.Output
         End If
      End Function

      <System.Runtime.CompilerServices.Extension()> _
      Public Function OutputTryCast(ByVal netType As NetTypeInfo, ByVal source As String) As String
         'Return netType.OutputTryCast(source)
         Return _
<code>TryCast(<%= source %>, <%= netType.Output %>)</code>.Value
      End Function

   End Module
End Namespace

Namespace CSharp
   Public Module NetTypeInfoExtensions
      <System.Runtime.CompilerServices.Extension()> _
      Public Function Output(ByVal netType As NetTypeInfo) As String
         If netType.TypeInfo IsNot Nothing Then
            Return netType.TypeInfo.OutputName
         Else
            Return netType.StandardNetType.Output
         End If
      End Function

      <System.Runtime.CompilerServices.Extension()> _
       Public Function OutputTryCast(ByVal netType As NetTypeInfo, ByVal source As String) As String
         Return _
<code><%= source %> as <%= netType %></code>.Value
      End Function

   End Module
End Namespace
